import http from '../../../class/Utils/Http.js'
import tips from '../../../class/Utils/Tips.js'
import user from '../../../class/Utils/User.js'
const app = getApp()
Page({
    data: {
        hyname: '全部行业',
        liuyantag: false,
        selecthangye: false,
        param: {'type': '0', 'cateid': '0', 'page': '1'},
        list: [],
        hangye: [],
        liuyan_list_state: false,
        nodata: 1,
        xiaoxi_count: 0
    },
    onLoad: function () {
        var that = this
        that.nav(that.data.param)
        //console.log(that.data.list);


    },
    onShow:function () {
        var that = this
        var user = wx.getStorageSync('user')
        if (user) {
            if (user.log == 1) {
                /*
                 获取未读消息数量
                 */
                http.req('/Mingpian/xiaoxi', {uid: user.userid}, function (res) {
                    console.log(res)
                    that.setData({
                        xiaoxi_count: res
                    })
                })
            }
        }
    },

    //搜索跳转页面
    searchlink: function (e) {
        wx.navigateTo({
            url: '/pages/weishangji/search/search'
        })
    },

    //切换滑动门
    typemenu: function (e) {
        var that = this
        var thisid = e.currentTarget.dataset.id
        var param = that.data.param
        param.type = thisid
        param.page = 1
        that.setData({
            param: param
        })

        that.nav(param)
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })
        that.setData({
            selecthangye: 0
        })

    },
    //打开留言
    liuyanbtn: function (e) {
        var that = this
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        user.log_user(that, function () {
            list[index].liuyan_state = !list[index].liuyan_state
            that.setData({
                list: list
            })
            /*获取留言内容*/
            if (list[index].liuyan_state) {
                var data = {
                    uid: wx.getStorageSync('user').userid,
                    mid: mid
                }
                http.reqload("/Api/liuyan_list", data, function (res) {
                    // console.log(res)
                    list[index].liuyan_list = res
                    if (list[index].liuyan_list.length > 5) {
                        list[index].liuyan_list.splice(5)
                        list[index].liuyan_gengduo = 1
                    }
                    that.setData({
                        list: list
                    })
                })
            }

        })

    },
    /*
     输入内容
     */
    content: function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        list[index].liuyan_content = e.detail.value
        this.setData({
            list: list
        })
    },
    liuyan: function (e) {
        var that = this
        var user = wx.getStorageSync("user")
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        if (list[index].liuyan_content == '') {
            tips.error('请输入留言内容')
            return
        }
        list[index].liuyan_disabled = true
        that.setData({
            list: list
        })
        var data = {
            uid: user.userid,
            content: list[index].liuyan_content,
            mid: mid
        }


        http.reqload("/Api/add_liuyan", data, function (res) {
            if (res.code == '202') {
                console.log(res);
                tips.ok(res.msg_val)
                list[index].liuyan_list.unshift({'poster': res.poster, 'content': res.content})
                list[index].liuyan_disabled = false
                list[index].liuyan_content = ''
                if (list[index].liuyan_list.length > 5) {
                    list[index].liuyan_list.splice(5)
                    list[index].liuyan_gengduo = 1
                }
                that.setData({
                    list: list
                })
            } else {
                tips.error(res.msg_val)
            }
        })
        console.log(list)
    },
    selecthangyebtn: function (e) {
        var that = this
        var thistag = that.data.selecthangye
        if (thistag == false) {
            that.setData({
                selecthangye: true
            })
        } else {
            that.setData({
                selecthangye: false
            })
        }
        var hangye = that.data.hangye
        if (hangye.length == 0) {
            http.reqload("/syq/hangye", '', function (res) {
                console.log(res);
                that.setData({
                    hangye: res
                })
            })
        }
        console.log(that.data.hangye);
    },
    selecthangye: function (e) {
        var that = this
        var thisid = e.currentTarget.dataset.id
        var name = e.currentTarget.dataset.name
        var param = that.data.param
        param.type = 0
        param.cateid = thisid
        param.page = 1
        that.setData({
            param: param,
            hyname: name,
            selecthangye: false
        })
        that.nav(param)
    },
    nav: function (param) {
        console.log(param);
        var that = this;
        http.reqload("/syq/trade", param, function (res) {
            console.log(res.docs);
            if (res.docs.length < 1) {
                that.setData({
                    nodata: 2
                })
            } else {
                that.setData({
                    nodata: 1
                })
            }
            for (var i = 0; i < res.docs.length; i++) {
                res.docs[i].changduans = 0;
                res.docs[i].liuyan_state = 0;   // 留言是否展开
                res.docs[i].liuyan_gengduo = 0; //留言是否大于5条
                res.docs[i].liuyan_list = [];  //已留言内容
                res.docs[i].liuyan_content = ''; //正在留言内容
                res.docs[i].liuyan_disabled = false; //正在按钮状态
            }
            that.setData({
                list: res.docs
            })
        })
    },
    //长短 介绍切换
    changeintro: function (e) {
        var that = this
        var presentindex = e.currentTarget.dataset.index
        var datalist = that.data.list
        datalist[presentindex].changduans = !datalist[presentindex].changduans
        that.setData({
            list: datalist
        })
    },
    onReachBottom: function () {
        var that = this
        var thistag = that.data.selecthangye
        if (thistag == true) {
            return false;
        }

        wx.showLoading({
            title: '加载中',
        })
        var param = that.data.param
        param.page++
        that.setData({
            param: param
        })

        var list = that.data.list
        //that.nav(param);

        http.reqload("/syq/trade", param, function (res) {
            console.log(param);
            for (var i = 0; i < res.docs.length; i++) {
                res.docs[i].changduans = 0;
                res.docs[i].liuyan_state = 0;   // 留言是否展开
                res.docs[i].liuyan_gengduo = 0; //留言是否大于5条
                res.docs[i].liuyan_list = [];  //已留言内容
                res.docs[i].liuyan_content = ''; //正在留言内容
                res.docs[i].liuyan_disabled = false; //正在按钮状态
                list.push(res.docs[i])
            }
            that.setData({
                list: list
            })
            wx.hideLoading()
        })
    },
    /*
     消息
     */
    xiaoxi: function () {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            tips.goto("/pages/mingpian/talk/list/list");
        })
    },
    /*
     发布商机链接
     */
    fabu: function () {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            tips.goto("/pages/weishangji/add/add");
        })
    },
    /*
     点击名片
     */
    mingpian: function (e) {
        var that = this
        user.log_user(this, function (res) {
            var poster = e.currentTarget.dataset.poster
            var cid = e.currentTarget.dataset.cid
            if (cid) {
                var dd = {
                    poster: poster,
                    cid: cid
                }
                console.log(dd)
                http.reqload("/Api/mingpian", dd, function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        tips.goto('/pages/mingpian/card/card?id=' + res.mid)
                    } else {
                        tips.error(res.msg_val)
                    }
                })
            } else {
                tips.error("暂无名片")
            }

        })
    },
    /*
     收藏
     */
    shoucang: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            var index = e.currentTarget.dataset.index
            var id = e.currentTarget.dataset.id
            var list = that.data.list
            var data = {
                uid: res.userid,
                tid: id
            }
            http.reqload("/Api/add_shoucang", data, function (res) {
                if (res.code == '203') {
                    list[index].shoucang = 1
                    that.setData({
                        list
                    })
                } else {
                    tips.error(res.msg_val)
                }
            })
        })

    },
    /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },

    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.reqload("/Api/log", {
            login: zhanghao,
            passwd: mima,
            uid: wx.getStorageSync('user').userid
        }, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    //点击图片放大
    previewImage: function (e) {
        var that = this;
        console.log(e)


        //获取点击的数据里的图片

        var thisindex = e.currentTarget.dataset.index
        var thatdatalist = that.data.list
        var thatdatalistindex = thatdatalist[thisindex].pic_list_img


        var newimgdata = [];
        for (var i = 0; i < thatdatalistindex.length; i++) {
            if (thatdatalistindex[i]) {
                newimgdata.push('http://img.i.album.toocle.com/0-0-0/' + thatdatalistindex[i]);
            }

        }
        that.setData({
            newimgdata: newimgdata,
        });

        console.log(newimgdata)


        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: that.data.newimgdata // 需要预览的图片http链接列表
        })
    },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },

})
